package com.kg.rails.component.editor.shape;

import com.kg.rails.component.editor.canvas.Graphics2DCanvas;
import com.kg.rails.component.editor.util.Constants;
import com.kg.rails.component.editor.util.EditorPoint;
import com.kg.rails.component.editor.util.EditorUtils;
import com.kg.rails.component.editor.view.CellState;

public class LineShape extends BasicShape {

    /**
     *
     */
    public void paintShape(Graphics2DCanvas canvas, CellState state) {
        if (configureGraphics(canvas, state, false)) {
            boolean rounded = EditorUtils.isTrue(state.getStyle(),
                    Constants.STYLE_ROUNDED, false)
                    && canvas.getScale() > Constants.MIN_SCALE_FOR_ROUNDED_LINES;

            canvas.paintPolyline(createPoints(canvas, state), rounded);
        }
    }

    /**
     *
     */
    public EditorPoint[] createPoints(Graphics2DCanvas canvas, CellState state) {
        String direction = EditorUtils.getString(state.getStyle(),
                Constants.STYLE_DIRECTION, Constants.DIRECTION_EAST);

        EditorPoint p0, pe;

        if (direction.equals(Constants.DIRECTION_EAST)
                || direction.equals(Constants.DIRECTION_WEST)) {
            double mid = state.getCenterY();
            p0 = new EditorPoint(state.getX(), mid);
            pe = new EditorPoint(state.getX() + state.getWidth(), mid);
        } else {
            double mid = state.getCenterX();
            p0 = new EditorPoint(mid, state.getY());
            pe = new EditorPoint(mid, state.getY() + state.getHeight());
        }

        EditorPoint[] points = new EditorPoint[2];
        points[0] = p0;
        points[1] = pe;

        return points;
    }

}
